home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- Newsgroups: comp.lang.c++
- Subject: Re: locking
- Date: Wed, 10 Jan 1996 17:30:59 +0100
- Organization: Fachbereich Informatik, TH Darmstadt
- Message-ID: <30F3E9C3.15FB7483@intellektik.informatik.th-darmstadt.de>
- References: <4d0j6r$1ri@daphne.ecmwf.int>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m)
-
- Baudouin Raoult wrote:
- >
- > I have an interesting problem. I would like to create an object
- > in shared memory, and lock/unlock it when accessing it:
- >
- > class foo {
- > public:
- > void bar();
- > };
- >
- > void main()
- > {
- > SharedObject<foo> fooH;
- >
- > fooH->bar();
- > }
- >
- > When calling foo::bar, a lock must be set and reset. Using the
- > operator-> does not help, because I cannot write
- >
- > SharedObject::operator->()
- > {
- > lock();
- > return object;
- > unlock();
- > }
- >
- > because the unlock is never executed. I solved my problem by adding
- > a temporary object:
- >
- > SharedObject::operator->()
- > {
- > return LockObject(object);
- > }
- >
- > with
- >
- > LockObject::LockObject(o) : object(o)
- > {
- > lock();
- > }
- >
- > LockObject::~LockObject()
- > {
- > unlock();
- > }
- >
- > LockObject::operator->()
- > {
- > return object;
- > }
- >
- > This seems to work, but the LockObject is only destroyed at the
- > end of the block, locking my object for too long.
- >
- > main()
- > {
- >
- > SharedObject<foo> fooH("lock");
- >
- > ... the lock is not set
- >
- > fooH->bar();
- >
- > .. the lock is set until the end.
- > }
- >
- > Anyone got an idea ?
- >
-
- I would say the temporary object should be destroyed directly after
- the invocation of 'bar'.
- As a workarround you can put the member-function call in brackets, e.g.
-
- { foo->bar(); }
-
- Enno
-